﻿using BrnMall.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using BrnMall.Manager.Manager.Cart;
using BrnMall.Manager.Manager.Restaurant;
using QIDong.WeApp.Models;
using WeiTap.Restful;
using System.Text;
using BrnMall.Core;

namespace QIDong.WeApp.Controllers
{
    /// <summary>
    /// 打印控制器
    /// </summary>
    public class PrintController : WebApi
    {
        #region 打印餐饮数据
        /// <summary>
        /// 获取客户确定的购物车数据（针对餐饮）
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public List<List<PrintOrderProductInfo>> GetStoreSessionPrintProductInfos(int storeId)
        {
            var ds = DiningTableManager.GetStoreDiningTables(storeId);
            return (from diningTable in ds
                    select Carts.GetCartProductList(diningTable.Id.ToString()).Where(t => CartManager.CheckIsPrint(t.RecordId) && !CartManager.CheckPrinted(t.RecordId)).ToList()
                into ps
                    where ps.Count != 0
                    select ps.Select(info => new PrintOrderProductInfo
                    {
                        AddTime = info.AddTime,
                        BookEndTime = info.BookEndTime,
                        BookNumber = info.BookNumber,
                        BookStartTime = info.BookStartTime,
                        BrandId = info.BrandId,
                        BuyCount = info.BuyCount,
                        CateId = info.CateId,
                        CostPrice = info.CostPrice,
                        CouponTypeId = info.CouponTypeId,
                        CurrProductSkuList = Products.GetProductSkuItemInfoList(info.Pid),
                        DiscountPrice = info.DiscountPrice,
                        ExtCode1 = info.ExtCode1,
                        ExtCode2 = info.ExtCode2,
                        ExtCode3 = info.ExtCode3,
                        ExtCode4 = info.ExtCode4,
                        ExtCode5 = info.ExtCode5,
                        IsReview = info.IsReview,
                        MarketPrice = info.MarketPrice,
                        Name = info.Name,
                        Oid = info.Oid,
                        PayCredits = info.PayCredits,
                        Pid = info.Pid,
                        PSN = info.PSN,
                        RealCount = info.RealCount,
                        RecordId = info.RecordId,
                        SendCount = info.SendCount,
                        ShopPrice = info.ShopPrice,
                        ShowImg = info.ShowImg,
                        Sid = info.Sid,
                        DiningTable = DiningTableManager.GetDiningTable(TypeHelper.StringToInt(info.Sid)),
                        StoreCid = info.StoreCid,
                        StoreId = info.StoreId,
                        StoreSTid = info.StoreSTid,
                        Type = info.Type,
                        Uid = info.Uid,
                        Weight = info.Weight
                    })
                        .ToList()).ToList();
        }
        /// <summary>
        /// 设置当前的记录已经打印
        /// </summary>
        /// <param name="recoredId"></param>
        public void SetSessionRecordPrinted(int recoredId)
        {
            CartManager.UpdateRecoredPrinted(recoredId);
        }
        #endregion
        /// <summary>
        /// 获取订单打印数据
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="startOrderState">
        /// 20:"待付款";40:"确认中";60:"已确认";80:"备货中";
        /// 100:"已发货";120:"已收货";140:"已锁定";160:"已取消";</param>
        /// <param name="endOrderState">订单结束</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="pageSize">每页调试 </param>
        public List<PrintOrderModel> GetPrintOrderList(int storeId, int startOrderState = 0, int endOrderState = 0, int pageNumber = 1, int pageSize = 15)
        {
            string condition = BrnMall.Manager.Manager.Order.OrderManager.GetOrderListCondition(storeId, string.Empty, 0, string.Empty, startOrderState, endOrderState, string.Empty);
            var table = AdminOrders.GetOrderList(pageSize, pageNumber, condition);
            return (from DataRow dr in table.Rows
                    select TypeHelper.ObjectToInt(dr["oid"])
                into oid
                    where oid != PrintRecords.GetPrintRecordsByOid(storeId, oid)
                    let orderInfo = Orders.GetOrderByOid(oid)
                    let ps = Orders.GetOrderProductList(oid)
                    let newPs = ps.Select(info => new PrintOrderProductInfo
                    {
                        AddTime = info.AddTime,
                        BookEndTime = info.BookEndTime,
                        BookNumber = info.BookNumber,
                        BookStartTime = info.BookStartTime,
                        BrandId = info.BrandId,
                        BuyCount = info.BuyCount,
                        CateId = info.CateId,
                        CostPrice = info.CostPrice,
                        CouponTypeId = info.CouponTypeId,
                        CurrProductSkuList = Products.GetProductSkuItemInfoList(info.Pid),
                        DiscountPrice = info.DiscountPrice,
                        ExtCode1 = info.ExtCode1,
                        ExtCode2 = info.ExtCode2,
                        ExtCode3 = info.ExtCode3,
                        ExtCode4 = info.ExtCode4,
                        ExtCode5 = info.ExtCode5,
                        IsReview = info.IsReview,
                        MarketPrice = info.MarketPrice,
                        Name = info.Name,
                        Oid = info.Oid,
                        PayCredits = info.PayCredits,
                        Pid = info.Pid,
                        PSN = info.PSN,
                        RealCount = info.RealCount,
                        RecordId = info.RecordId,
                        SendCount = info.SendCount,
                        ShopPrice = info.ShopPrice,
                        ShowImg = info.ShowImg,
                        Sid = info.Sid,
                        DiningTable = DiningTableManager.GetDiningTable(TypeHelper.StringToInt(info.Sid)),
                        StoreCid = info.StoreCid,
                        StoreId = info.StoreId,
                        StoreSTid = info.StoreSTid,
                        Type = info.Type,
                        Uid = info.Uid,
                        Weight = info.Weight
                    })
                        .ToList()
                    select new PrintOrderModel()
                    {
                        OrderInfo = orderInfo,
                        RegionInfo = Regions.GetRegionById(orderInfo.RegionId),
                        OrderProductList = newPs,
                    }).ToList();
        }

        /// <summary>
        /// 插入订单已打印
        /// </summary>
        /// <param name="storeid"></param>
        /// <param name="printstatus"></param>
        /// <param name="oid"></param>
        public void SetPrintStatus(int storeid, int printstatus = -1, int oid = -1)
        {
            PrintRecords.InsertPrintRecord(new PrintRecord
            {
                StoreID = storeid,
                Oid = oid,
                PrintID = 0,
                PrintState = printstatus
            });
        }
    }
}